package com.hbk.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hbk.base.common.CommonResult;
import com.hbk.base.common.Ref;
import com.hbk.base.util.TimeUtil;
import com.hbk.entity.SchoolItem;
import com.hbk.mapper.SchoolItemMapper;
import com.hbk.param.SchoolItemParam;
import com.hbk.service.ICacheService;
import com.hbk.service.IOssService;
import com.hbk.service.ISchoolItemService;
import com.hbk.vo.SchoolItemVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SchoolItemServiceImpl implements ISchoolItemService {

    @Resource
    private SchoolItemMapper schoolItemMapper;
    @Resource
    private IOssService ossService;
    @Resource
    private ICacheService cacheService;

    public CommonResult add(SchoolItemParam.AddParam param){
        SchoolItem item = new SchoolItem();
        item.setImgUrl(param.getImgUrl());
        item.setTitle(param.getTitle());
        item.setLinkUrl(param.getLinkUrl());
        item.setOrderNum(param.getOrderNum());
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        schoolItemMapper.insert(item);
        cacheService.deleteIndexCache();
        return Ref.ok();
    }

    public CommonResult modify(SchoolItemParam.ModifyParam param){
        SchoolItem item = schoolItemMapper.selectById(param.getId());
        if(item==null){
            return Ref.error("找不到该院校信息");
        }
        item.setImgUrl(param.getImgUrl());
        item.setTitle(param.getTitle());
        item.setLinkUrl(param.getLinkUrl());
        item.setOrderNum(param.getOrderNum());
        item.setUpdateTime(new Date());
        schoolItemMapper.updateById(item);
        cacheService.deleteIndexCache();
        return Ref.ok();
    }

    public CommonResult listAll(){
        List<SchoolItem> itemList = schoolItemMapper.listAll();
        List<SchoolItemVo.ListAllVo> vos = new ArrayList<>();
        for(SchoolItem item : itemList){
            SchoolItemVo.ListAllVo vo = new SchoolItemVo.ListAllVo();
            vo.setId(item.getId());
            vo.setImgUrl(item.getImgUrl());
            vo.setTitle(item.getTitle());
            vo.setLinkUrl(item.getLinkUrl());
            vo.setOrderNum(item.getOrderNum());
            vo.setCreateTime(TimeUtil.formaterYYYYMMddHHmm(item.getCreateTime()));
            vo.setUpdateTime(TimeUtil.formaterYYYYMMddHHmm(item.getUpdateTime()));
            vos.add(vo);
        }
        JSONArray array = new JSONArray();
        JSONObject json = null;
        for(int i=0; i<vos.size(); i++){
            if(i%6==0){
                json = new JSONObject();
                json.put("id", i);
                json.put("data", new JSONArray());
                array.add(json);
            }
            JSONArray data = json.getJSONArray("data");
            data.add(vos.get(i));
        }
        return Ref.ok(array);
    }

    public CommonResult delete(SchoolItemParam.DeleteParam param){
        SchoolItem item = schoolItemMapper.selectById(param.getId());
        if(item==null){
            return Ref.ok();
        }
        ossService.delete(item.getImgUrl());
        schoolItemMapper.deleteById(param.getId());
        cacheService.deleteIndexCache();
        return Ref.ok();
    }
}
